﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;

namespace C
{
    public class Product_PropertyFilterController
    {
        public static void Insert(Product_PropertyFilter a)
        {
            // Neu da ton tai thi khong insert
            if (Get(a.ProductID, a.PropertyFilterID) != null) return;

            HCDataContext db = new HCDataContext();
            db.Product_PropertyFilters.InsertOnSubmit(a);
            db.SubmitChanges();
        }

        public static void Update(Product_PropertyFilter a, HCDataContext db)
        {
            db.SubmitChanges();
        }

        public static void Delete(int id)
        {
            HCDataContext db = new HCDataContext();
            Product_PropertyFilter a = db.Product_PropertyFilters.Where(
                ik => ik.Product_PropertyFilterID == id).First();

            // Xoa record trong database
            db.Product_PropertyFilters.DeleteOnSubmit(a);
            db.SubmitChanges();
        }

        public static Product_PropertyFilter GetByID(int id, HCDataContext db)
        {
            return db.Product_PropertyFilters.Where(
                ik => ik.Product_PropertyFilterID == id).FirstOrDefault();
        }

        public static Product_PropertyFilter GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }

        public static Product_PropertyFilter Get(int productID, int propertyFilterID)
        {
            return new HCDataContext().Product_PropertyFilters.Where(ik =>
                ik.ProductID == productID
                && ik.PropertyFilterID == propertyFilterID).FirstOrDefault();
        }

        public static List<Product_PropertyFilter> GetList(int productId, int propertyID)
        {
            return new HCDataContext().Product_PropertyFilters.Where(ik =>
                ik.ProductID == productId
                && ik.PropertyFilter.ProductPropertyID == propertyID).ToList();
        }

        public static List<Product_PropertyFilter> GetList(List<int> listPropertyFilterID)
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Product_PropertyFilters
                         where listPropertyFilterID.Contains(a.PropertyFilterID)
                         select a;
            return result.ToList();
        }

        public static void Clear(int productId, int propertyID)
        {
            foreach (Product_PropertyFilter ppf in GetList(productId, propertyID))
            {
                Delete(ppf.Product_PropertyFilterID);
            }
        }

        public static void Inserts(int productId, List<int> listFilterID)
        {
            foreach (int filterID in listFilterID)
            {
                Product_PropertyFilter ppf = new Product_PropertyFilter();
                ppf.ProductID = productId;
                ppf.PropertyFilterID = filterID;
                Insert(ppf);
            }
        }
    }
}